package com.hontech.bv.ui.fragment.pick

import android.view.View
import android.widget.Button
import com.hontech.bv.R
import com.hontech.bv.conn.Device
import com.hontech.bv.conn.api.Pick
import com.hontech.bv.task.appScope
import com.hontech.bv.task.onClick
import com.hontech.bv.ui.view.SwitchCtrlView
import com.hontech.bv.ui.window.showProg
import com.hontech.bv.ui.wrapDialog
import com.hontech.bv.utils.openOrClose
import kotlinx.coroutines.launch

class CtrlDelegate (view: View) {

    private val btnOpen1 = view.findViewById<Button>(R.id.id_pick_btn_1_open)
    private val btnClose1 = view.findViewById<Button>(R.id.id_pick_btn_1_close)

    private val btnOpen2 = view.findViewById<Button>(R.id.id_pick_btn_2_open)
    private val btnClose2 = view.findViewById<Button>(R.id.id_pick_btn_2_close)

    private val btnOpenStraw = view.findViewById<Button>(R.id.id_pick_btn_straw_open)
    private val btnCloseStraw = view.findViewById<Button>(R.id.id_pick_btn_straw_close)

    private val swc1 = view.findViewById<SwitchCtrlView>(R.id.id_pick_swc_led1)
    private val swc2 = view.findViewById<SwitchCtrlView>(R.id.id_pick_swc_led2)
    private val swcStraw = view.findViewById<SwitchCtrlView>(R.id.id_pick_swc_led_straw)

    private val btnInit = view.findViewById<Button>(R.id.id_pick_btn_init)
    private val btnPick1 = view.findViewById<Button>(R.id.id_pick_btn_pick1)
    private val btnPick2 = view.findViewById<Button>(R.id.id_pick_btn_pick2)

    init {
        btnInit.onClick {
            wrapDialog("取物门", "初始化") {
                Pick.init()
            }
        }

        btnPick1.onClick {
            wrapDialog("取物门1", "出货") {
                Pick.pick(1)
            }
        }

        btnPick2.onClick {
            wrapDialog("取物门2", "出货") {
                Pick.pick(2)
            }
        }

        btnOpen1.onClick {
            wrapDialog("取物门1", "打开") {
                Pick.doorCtrl(1, 0)
            }
        }
        btnClose1.onClick {
            wrapDialog("取物门1", "关闭") {
                Pick.doorCtrl(1, 1)
            }
        }

        btnOpen2.onClick {
            wrapDialog("取物门1", "打开") {
                Pick.doorCtrl(2, 0)
            }
        }
        btnClose2.onClick {
            wrapDialog("取物门2", "关闭") {
                Pick.doorCtrl(2, 1)
            }
        }

        btnOpenStraw.onClick {
            wrapDialog("吸管盒", "打开") {
                Pick.doorCtrl(3, 0)
            }
        }
        btnCloseStraw.onClick {
            wrapDialog("吸管盒", "关闭") {
                Pick.doorCtrl(3, 1)
            }
        }

        swc1.clickCb = {
            val value = if (it) 0 else 1
            appScope.launch {
                wrapDialog("取物门1灯", "打开/关闭") {
                    Pick.ledCtrl(1, value)
                }
            }
        }

        swc2.clickCb = {
            val value = if (it) 0 else 1
            appScope.launch {
                wrapDialog("取物门2灯", "打开/关闭") {
                    Pick.ledCtrl(2, value)
                }
            }
        }

        swcStraw.clickCb = {
            val value = if (it) 0 else 1
            appScope.launch {
                wrapDialog("吸管盒灯", "打开/关闭") {
                    Pick.ledCtrl(3, value)
                }
            }
        }
    }


}